// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.netease.yunxin.kit.chatkit.ui.custom;

import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachmentParser;
import com.netease.yunxin.kit.chatkit.ui.ChatMessageType;
import org.json.JSONObject;

public class CustomAttachParser implements MsgAttachmentParser {

  private static final String KEY_TYPE = "type";
  private static final String KEY_INFO = "info";

  @Override
  public MsgAttachment parse(String json) {
    CustomAttachment attachment = null;
    try {
      JSONObject object = new JSONObject(json);
      int type = object.getInt(KEY_TYPE);
      JSONObject data = object.getJSONObject(KEY_INFO);
      if (type == ChatMessageType.CUSTOM_STICKER) {
        attachment = new StickerAttachment();
      } else if(type == ChatMessageType.CUSTOM_GIFT) {
        attachment = new GiftAttachment();
      }

      if (attachment != null) {
        attachment.fromJson(data);
      }
    } catch (Exception e) {

    }

    return attachment;
  }

  public static String packData(int type, JSONObject data) {
    JSONObject object = new JSONObject();
    try {
      object.put(KEY_TYPE, type);
      if (data != null) {
        object.put(KEY_INFO, data);
      }

    } catch (Exception exception) {

    }

    return object.toString();
  }
}
